﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.Sell.Infacet
{
    public class BaseReository<T> : IBaseRepository<T> where T : class, new()
    {
        private readonly DbcontextWMS _db;

        public BaseReository(DbcontextWMS db)
        {
            _db = db;
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(T entity)
        {
            _db.Set<T>().Add(entity);
            return _db.SaveChanges();
        }
        /// <summary>
        /// 删除实体111
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Delete(T entity)
        {
            _db.Set<T>().Remove(entity);
            return _db.SaveChanges();
        }
        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(T entity)
        {
            _db.Set<T>().Update(entity);
            return _db.SaveChanges();
        }
        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return _db.Set<T>().AsQueryable();
        }

        public Task<int> UpdateAsync(T entity)
        {
            _db.Set<T>().Update(entity);
            return _db.SaveChangesAsync();
        }

        public async Task<T> GetModel(int t)
        {
            return await _db.Set<T>().FindAsync(t);
        }
        public async Task<IQueryable<T>> GetAllAsync()
        {
            return  _db.Set<T>();
        }
        public Task<int> DelRange(List<T> t)
        {
            _db.Set<T>().RemoveRange(t);
            return _db.SaveChangesAsync();
        }

        public Task<int> AddRange(List<T> t)
        {
            _db.Set<T>().AddRange(t);
            return _db.SaveChangesAsync();
        }
        public Task<int> AddAsync(T entity)
        {
            _db.Set<T>().AddAsync(entity);
            return _db.SaveChangesAsync();
        }
    }
}
